﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace OutMemory
{
    class Program
    {
        static void DisplayMemory()
        {
            Console.WriteLine("Total memory: {0:###,###,###,##0} bytes", GC.GetTotalMemory(true));
        }

        private static readonly int[] m_loopCount = new int[8];

        private static int m_WRCounter = -1;

        public static void TryDequeue_Result()
        {
            var queue = new ConcurrentQueue<string>();
            queue.Enqueue("foo1");
            queue.Enqueue("foo2");
            queue.Enqueue("foo");
            queue.Enqueue("foo");

            string element;
            bool result = queue.TryDequeue(out element);


        }
        static void Main()
        {
            while (true)
            {
                for (int i = 0; i < 8; i++)
                {
                    m_loopCount[i]++;
                }

                Thread.Sleep(10);
            }

            TryDequeue_Result();

            bool IsOverrun = true;

            IsOverrun &= 5 >= 2;

            var k = Interlocked.Add(ref m_WRCounter, 1);

            //DisplayMemory();
            //Console.WriteLine();
            //TestClassHasEvent hasevent = new TestClassHasEvent();
            //for (int i = 0; i < 10; i++)
            //{
            //    Console.WriteLine("--- New Listener #{0} ---", i + 1);
            //    var listener = new TestListener(hasevent);
            //    // var listener = new TestListener(new TestClassHasEvent());
            //    GC.Collect();
            //    GC.WaitForPendingFinalizers();
            //    GC.Collect();
            //    DisplayMemory();

            //}
            //Console.Read();
            // Test();

            ConcurrentDictionary<int, A> dic = new ConcurrentDictionary<int, A>();
            dic.TryAdd(1, new A() { value = 1 });
            dic.TryAdd(2, new A() { value = 2 });

            A a;
            bool flag;
            flag = dic.TryRemove(2, out a);
            Console.WriteLine(flag);
            if (a == null)
                Console.WriteLine("a==null");
            else
            {
                Console.WriteLine($"a.value={a.value}");
            }

            flag = dic.TryRemove(3, out a);
            Console.WriteLine(flag);
            if (a == null)
                Console.WriteLine("a==null");

        }

        static object ob = new object();

        private static void Test()
        {
            int i = 0;
            new Thread(() =>
            {
                lock (ob)
                {
                    for (int k = 0; k < 10; k++)
                    {
                        i++;
                    }
                }


            }).Start();

            new Thread(() =>
            {
                lock (ob)
                {
                    for (int k = 0; k < 10; k++)
                    {
                        i++;
                    }
                }
            }).Start();

            Console.WriteLine(i);
        }
    }
}
